﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MODULE_KCS
{
    
    public partial class frm_inphieubiennhan : Form
    {
        private string sophieu;
        public string _sophieu
        {
            get { return sophieu; }
            set { sophieu = value; }
        }
        kcsDataContext context = cls_kcs.getcontext();
        public frm_inphieubiennhan()
        {
            InitializeComponent();
        }

        private void frm_choncongty_Load(object sender, EventArgs e)
        {
            IEnumerable<DM_CongTy> congtys = from c in context.DM_CongTies select c;
            
            this.cbcongty.DataSource = congtys;
            this.cbcongty.ValueMember = "macongty";
            this.cbcongty.DisplayMember = "tencongty";

        }

        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                IEnumerable<NX_phieunhaphang_h> phieunhaphs = from c in context.NX_phieunhaphang_hs where c.sophieu.Equals(sophieu) select c;
                if (phieunhaphs.Count() > 0)
                {
                    NX_phieunhaphang_h phieunhaph = phieunhaphs.ElementAt(0);
                    //string machuhang = txtmachuhang.Text;
                    string connectstring = context.Connection.ConnectionString;
                    SqlConnection connection = new SqlConnection(connectstring);
                    SqlCommand command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "select * from phieubiennhan_h where sophieu='" + sophieu + "'";

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable tb = new DataTable("biennhan_h");
                    adapter.Fill(tb);
                    DataSet ds = new DataSet();
                    ds.Tables.Add(tb);


                    MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();
                    MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                    //DataTable dth = cls.IEnumerabletoDataTable(phieunhaphs);

                    string macongty = cbcongty.SelectedValue.ToString();
                    if (phieunhaph.loaimi == "1")
                    {
                        command = new SqlCommand();
                        command.Connection = connection;
                        command.CommandText = "select * from phieubiennhan_mituoi_l where sophieu='" + sophieu + "'";
                        DataTable tbl = new DataTable("biennhan_mituoi_l");
                        adapter = new SqlDataAdapter(command);
                        adapter.Fill(tbl);
                        ds.Tables.Add(tbl);
                        Cursor.Current = Cursors.WaitCursor;
                        if (macongty == constant.DAIVIET)
                        {
                            objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mituoi_daiviet.rpt", ds);
                        }
                        if (macongty == constant.VANXUAN)
                        {
                            objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mituoi_vanxuan.rpt", ds);
                        }
                       
                        //IEnumerable<phieubiennhan_mituoi_l> phieunhapls=from c in context.phieubiennhan_mituoi_ls where c.sophieu.Equals(sophieu) select c;

                        //objReport.Load_SubReport("biennhan_mituoi_l", tbl);

                    }
                    else
                    {
                        if (phieunhaph.loaimi == "2")
                        {
                            command = new SqlCommand();
                            command.Connection = connection;
                            command.CommandText = "select * from phieubiennhan_mikho_l where sophieu='" + sophieu + "'";
                            DataTable tbl = new DataTable("biennhan_mikho_l");
                            adapter = new SqlDataAdapter(command);
                            adapter.Fill(tbl);
                            ds.Tables.Add(tbl);
                            Cursor.Current = Cursors.WaitCursor;
                            if (macongty == constant.DAIVIET)
                            {
                                objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mikho_daiviet.rpt", ds);

                            }//IEnumerable<phieubiennhan_mikho_l> phieunhapls = from c in context.phieubiennhan_mikho_ls where c.sophieu.Equals(sophieu) select c;
                            if (macongty == constant.VANXUAN)
                            {
                                objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mikho_vanxuan.rpt", ds);

                            }
                            //objReport.Load_SubReport("biennhan_mikho_l", tbl);
                            
                        }
                        else
                        {
                            Cursor.Current = Cursors.WaitCursor;
                            if (macongty == constant.DAIVIET)
                            {
                                objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mathangkhac_daiviet.rpt", tb);
                            }
                            if (macongty == constant.VANXUAN)
                            {
                                objReport.Load_Report(Application.StartupPath + "\\reports\\biennhan_mathangkhac_vanxuan.rpt", tb);
                            }
                            //IEnumerable<phieubiennhan_mikho_l> phieunhapls = from c in context.phieubiennhan_mikho_ls where c.sophieu.Equals(sophieu) select c;
                            command = new SqlCommand();
                            command.Connection = connection;
                            command.CommandText = "select * from phieubiennhan_mathangkhac where sophieu='" + sophieu + "'";
                            DataTable tbl = new DataTable();
                            //adapter = new SqlDataAdapter(command);
                            adapter.Fill(tbl);

                        }
                    }
                    DateTime d = context.p_getdate().ElementAt(0).serverdatetime;

                    if (d.Day < 10)
                    {
                        objReport.Formulas_Report("ngay", "'0" + d.Day.ToString() + "'");
                    }
                    else
                    {
                        objReport.Formulas_Report("ngay", "'" + d.Day.ToString() + "'");
                    }
                    if (d.Month < 10)
                    {
                        objReport.Formulas_Report("thang", "'0" + d.Month.ToString() + "'");
                    }
                    else
                    {
                        objReport.Formulas_Report("thang", "'" + d.Month.ToString() + "'");
                    }

                    objReport.Formulas_Report("nam", "'" + d.Year.ToString() + "'");

                    IEnumerable<DM_CongTy> congtys = from c in context.DM_CongTies where c.MaCongTy.Equals(macongty) select c;
                    if (congtys.Count() > 0)
                    {
                        DM_CongTy congty = congtys.ElementAt(0);
                        objReport.Formulas_Report("tencongty", "'" + congty.TenCongTy + "'");
                        objReport.Formulas_Report("diachicongty", "'" + congty.DiaChi + "'");
                        objReport.Formulas_Report("dienthoaicongty", "'" + congty.DienThoai + "'");
                    }
                    objReport.Open_Report();
                    Cursor.Current = Cursors.Default;
                    ////objCtrl = Nothing
                    //objReport = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }

    
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
